package com.carlinfo.stu_ques.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{

	private String dateStr = "yyyy-MM-dd";
	private String dateTimeStr = "yyyy-MM-dd HH:mm:ss";

	public String dateToStr(Date date)
	{

		SimpleDateFormat sdf = new SimpleDateFormat(dateStr);
		return sdf.format(date);
	}

	public String dateToStr(Date date, String pattern)
	{

		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public String dateTimeToStr(Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateTimeStr);
		return sdf.format(date);
	}

	public Date strToDate(String str)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateStr);
		try
		{
			return sdf.parse(str);
		} catch (ParseException e)
		{
			System.out.println("你输入的时间不对,已自动设置为当前时间");
		}
		return new Date();
	}

	public Date strToDate(String str, String pattern)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try
		{
			return sdf.parse(str);
		} catch (ParseException e)
		{
			System.out.println("你输入的时间不对,已自动设置为当前时间");
		}
		return new Date();
	}

	public Date strToDateTime(String str)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateTimeStr);
		try
		{
			return sdf.parse(str);
		} catch (ParseException e)
		{
			System.out.println("你输入的时间不对,已自动设置为当前时间");
		}
		return new Date();
	}

	/**
	 * 判断时间是不是昨天
	 * @param nowDate
	 * @param oldDate
	 * @return
	 */
	public boolean isYesterday(Date nowDate,Date oldDate)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowDate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		Date dateSt = calendar.getTime();
		return oldDate.before(dateSt);
	}
	public static void main(String[] args)
	{
		DateUtil dateUtil = new DateUtil();
		Date d = dateUtil.strToDateTime("2018-08-08 12:02:02");
		Date d2 = dateUtil.strToDateTime("2018-08-05 17:03:03");
		System.out.println(dateUtil.isYesterday(d, d2));
		
	}
}
